package com.t.bricks.bussiness.controller.wechat;

import io.swagger.annotations.Api;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import springfox.documentation.annotations.ApiIgnore;

import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.json.JacksonShell;
import com.t.bricks.utils.function.url.URLUtil;

/**
 * 请求转发
 */
@Controller
@ApiIgnore//swagger屏蔽此接口
@Api(value="请求转发", tags={"请求转发操作接口"})
@RequestMapping("/skip/")
public class SkipController {
		
	/**
	 * 进行内部转发
	 * 注意:内部转发不能添加域名,待转发的参数url不能用?连接参数,并且 只能转发到本应用的中的某些资源
	 * @param state 跳转的页面url
	 * @param sType 请求类型,默认POST
	 * @param redirectAttributes 重定向时设置参数
	 * @return
	 */
	@RequestMapping(value="/inside", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
    public void skipInside(String url, HttpServletRequest request,
    	HttpServletResponse response) {
		
		try {
			request.getRequestDispatcher(url).forward(request, response);
		} catch (Exception e) {
			Log.error("转发请求异常,请求地址:", url);
		}
    }
	
	/**
	 * GET方式进行内部跳转请求
	 * @param url 请求的页面地址
	 * @return
	 */
	@RequestMapping(value="/read", method=RequestMethod.GET)
	@ResponseBody
    public String readGet(String url, HttpServletRequest request,
    	HttpServletResponse response) {
		if (null == url || "".equals(url.trim())) {
			return "";
		}
		
		StringBuilder sb = new StringBuilder(url);
		if (!url.contains("?")) {
			sb.append("?");
		}

		Map<String, String[]> map = request.getParameterMap();
		for (Map.Entry<String, String[]> entry : map.entrySet()) {
			sb.append(entry.getKey()).append("=").append(entry.getValue());
		}

		Enumeration<String> enums = request.getHeaderNames();
		Map<String, String> hearMap = new HashMap<String, String>();
		while (enums.hasMoreElements()) {
            String key = enums.nextElement();
			hearMap.put(key, request.getHeader(key));
        }
		
		MsgEmity me = URLUtil.get(url, hearMap);
		if (!me.isSuccess()) {
			return JacksonShell.toJSONString(me);
		}
		
		return me.getData();
    }
	
	/**
	 * POST方式进行内部跳转请求
	 * @param url 请求的页面地址
	 * @return
	 */
	@RequestMapping(value="/read", method=RequestMethod.POST)
	@ResponseBody
    public String readPost(String url, HttpServletRequest request,
    	HttpServletResponse response) {
		Map<String, Object> parames = new HashMap<String, Object>();
		for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
			parames.put(entry.getKey(), entry.getValue());   //把username参数传递到request中
		}
		
		if (url.contains("?")) {
			String[] temp = url.split("\\?");
			if (temp.length > 1) {
				String[] array = temp[1].split("=");
				parames.put(array[0], array[1]);
			}
		}

		Enumeration<String> enums = request.getHeaderNames();
		Map<String, String> hearMap = new HashMap<String, String>();
		while (enums.hasMoreElements()) {
            String key = enums.nextElement();
			hearMap.put(key, request.getHeader(key));
        }
		
		String tmp = request.getHeader("Content-type");
		if (null != tmp && tmp.contains("application/json")) {
			MsgEmity me = URLUtil.postJson(url, JacksonShell.toJSONString(parames), hearMap);
			if (!me.isSuccess()) {
				return "跳转失败";
			}
			
			return me.getData();
		}
		
		MsgEmity me = URLUtil.post(url, parames, hearMap);
		
		if (!me.isSuccess()) {
			return "跳转失败";
		}
		
		return me.getData();
    }
	
}
